#include <guiconstants.au3>
#include <EditConstants.au3>
#include <guiedit.au3>
#include <windowsConstants.au3>
#include <excel.au3>
#include <string.au3>

if StringInStr( WinGetTitle( "HP ServiceCenter" ), "\\Remote" ) Then
	MsgBox( 16, "autoServiceCenter", "Sorry, this utility only works with local ServiceCenter clients." & @CRLF & "This is due to remote citrix applications not having readable control IDs to get and set data." )
	Exit
EndIf

if NOT WinExists( "HP ServiceCenter" ) Then
	Dim $uEnc = "8E1C0A0A86FEE3128AE086ECB955"
	Dim $pEnc = "8E1C0B0A87F8E2618DE381E1B927B29AE91655DE23BBADC44D0D95AB62B776AF7277"
	
	TrayTip( "autoServiceCenter", "Trying to launch Service Center...", 3 )
	Dim $stsLaunch = ShellExecute( @DesktopDir & "\ServiceCenter Client.lnk" )
	if $stsLaunch Then
		$frmMain = GUICreate( "autoServiceCenter", 185, 200 )
		TrayTip( "autoServiceCenter", "Trying to launch Service Center... successful.", 3 )
		Sleep(5000)
		WinActivate( "Connections" )
		ControlSetText( "Connections", "", "Edit2", _StringEncrypt( 0, $uEnc, "" ) )
		ControlSetText( "Connections", "", "Edit3", _StringEncrypt( 0, $pEnc, "" ) )
		ControlClick( "Connections", "", "Button9" )
		WinWaitActive( "HP ServiceCenter - " & _StringEncrypt( 0, $uEnc, "" ) )
		Sleep(1500)
		ControlClick( "HP ServiceCenter", "", "SWT_Window041", 1, 1, 85, 9 )
		Sleep(500)
		ControlClick( "HP ServiceCenter", "", "Button21" )
		$hndlSC = WinGetHandle( "HP ServiceCenter" )
	Else
		TrayTip( "autoServiceCenter", "Trying to launch Service Center... failed.", 3 )
		Exit
	EndIf
Else
	$hndlSC = WinGetHandle( "HP ServiceCenter" )
	$posSC = WinGetPos( "HP ServiceCenter" )
	$frmMain = GUICreate( "autoServiceCenter", 185, 200, $posSC[0]+$posSC[2], $posSC[1])
EndIf

$btnVer = GUICtrlCreateButton( "Verify", 2, 2 )
$btnVerE = GUICtrlCreateButton( "Verify from Excel", 35, 2 )
$btnRetire = GUICtrlCreateButton( "Retire", 120, 2 )
$btnStop = GUICtrlCreateButton( "Stop", 157, 2 )
$chkStick = GUICtrlCreateCheckbox( "Stick to Service Center", 2, 182 )


$edtSerial = GUICtrlCreateEdit( "Serial", 2, 80, 140, 100, BitOR($ES_MULTILINE,$ES_WANTRETURN,$ES_AUTOVSCROLL,$WS_VSCROLL) )
$edtContact = GUICtrlCreateEdit( "Contact", 2, 30, 140, 21, $ES_AUTOHSCROLL )
$edtLoc = GUICtrlCreateEdit( "Location", 2, 55, 140, 21, $ES_AUTOHSCROLL )

GUICtrlSetTip( $btnRetire, "Retire the current configuration item." )
GUICtrlSetTip( $btnVer, "Verify an open configuration item." )

GUISetState( @SW_SHOW )

Dim $guiMsg
GUICtrlSetState( $chkStick, $GUI_CHECKED )

while 1
	$guiMsg = GUIGetMsg()
	$stick = GUICtrlRead( $chkStick )
	
	if $stick = $GUI_CHECKED AND WinExists( $hndlSC ) Then
		$posSC = WinGetPos( $hndlSC )
		WinMove( "autoServiceCenter", "", $posSC[0]+$posSC[2], $posSC[1] )
	EndIf
	
	; nice web 2.0-looking edit box formatting	
	#cs
	if GUICtrlRead( $edtSerial ) = "" OR GUICtrlRead( $edtSerial ) = "Serial" Then
		GUICtrlSetColor( $edtSerial, 0x999999 )
		GUICtrlSetData( $edtSerial, "Serial" )
	Else
		GUICtrlSetColor( $edtSerial, 0x000000 )
	EndIf
	
	if GUICtrlRead( $edtContact ) = "" OR GUICtrlRead( $edtContact ) = "Contact" Then
		GUICtrlSetColor( $edtContact, 0x999999 )
		GUICtrlSetData( $edtContact, "Contact" )
	Else
		GUICtrlSetColor( $edtContact, 0x000000 )
	EndIf
	
	if GUICtrlRead( $edtLoc ) = "" OR GUICtrlRead( $edtLoc ) = "Location" Then
		GUICtrlSetColor( $edtLoc, 0x999999 )
		GUICtrlSetData( $edtLoc, "Location" )
	Else
		GUICtrlSetColor( $edtLoc, 0x000000 )
	EndIf
	#ce
	
	Select
	Case $guiMsg = $GUI_EVENT_CLOSE
		GUIDelete()
		Exit
	Case $guiMsg = $btnRetire
		GUISetState(@SW_DISABLE)
		aSC_Retire()
		GUISetState(@SW_ENABLE)
	Case $guiMsg = $btnVer
		GUISetState(@SW_DISABLE)
		aSC_Verify()
		GUISetState(@SW_ENABLE)
		Sleep(2250)
	Case $guiMsg = $btnVerE
		
		aSC_VerifyFromExcel()
		
	EndSelect
	
	Sleep(10)
WEnd

Func aSC_VerifyFromExcel()
	Dim $serial, $contact, $location, $id, $clinical
	Dim $objExcel, $pathExcel, $arrSheet
	
	$pathExcel = FileOpenDialog( "Select excel worksheet...", "c:\", "Excel (*.xls)" )
	$objExcel = _excelbookopen( $pathExcel )
	
	$qTitle = MsgBox(4, "autoServiceCenter", "Is there a title row in the worksheet?" )
	
	if $qTitle = 6 Then
		$arrSheet = _ExcelReadSheetToArray( $objExcel, 2, 1 )
	Else
		$arrSheet = _ExcelReadSheetToArray( $objExcel )
	EndIf
	
	WinActivate( "HP ServiceCenter" )
	
	ProgressOn( "autoServiceCenter", "Verifying computers from " & FileGetLongName($pathExcel, 1), 5, 5, 16 )
	
	; Reference for quickbase export layout
	;
	; [x][1] - Department
	; [x][2] - Contact name
	; [x][3] - Location
	; [x][4] - Model
	; [x][5] - Asset
	; [x][6] - Serial
	; [x][7] - Clinical ID
	; [x][8] - Computer name
	; [x][9] - Comments
	; [x][10] - Tech	
	; [x][11] - Generic 1
	; [x][12] - use this for writing the existing clinical ID
	
	for $count = 1 to $arrSheet[0][0]
		; Check and stop if needed
		$guiMsg = GUIGetMsg()
		if $guiMsg = $btnStop Then
			ProgressOff()
			GUICtrlSetData( $edtSerial, "Stopped after " & $serial & "." )
			ExitLoop
		EndIf
		
		; Initialize variables from array
		$serial = $arrSheet[$count][6]
		$asset = $arrSheet[$count][5]
		$location = $arrSheet[$count][3]
		$contact = $arrSheet[$count][2]
		$id = $arrSheet[$count][7]
		$cname = $arrSheet[$count][8]
		
		; Sort of status display
		GUICtrlSetData( $edtContact, $contact )
		GUICtrlSetData( $edtLoc, $location )
		GUICtrlSetData( $edtSerial, $serial )
		
		; Progress bar
		ProgressSet( ($count/$arrSheet[0][0])*100, "Current computer " & $count & "/" & $arrSheet[0][0] & ": " & $serial )
		
		if $serial = "" Then
			; Exit if there's a blank serial to avoid getting stuck or whatever
			ProgressOff()
			ExitLoop
		EndIf
		
		; Set clinical if there's an ID and it doesn't contain "trn"
		if $id Then
			if NOT StringInStr( $id, "trn" ) Then $clinical = 1
		Else
			$clinical = 0
		EndIf
	
		; Activate SC search window, put in the serial and hit enter
		WinWaitActive( "HP ServiceCenter - Search" )
		Sleep(500)
		ControlSetText( "HP ServiceCenter - Search", "", "Edit1", $serial )
		Send( "{ENTER}" )
	
		; Wait for the config window to come up
		WinWaitActive( "HP ServiceCenter - Configuration Item:  " & $serial )
		Sleep(250)
		
		; Set contact person and auto-fill
		ControlSetText( "HP ServiceCenter", "", "Edit4", $contact )
		ControlClick( "HP ServiceCenter", "", "Button28" )
		Sleep(1500)
		
		; Set to installed
		ControlSetText( "HP ServiceCenter", "", "Edit34", "Installed" )
		
		; Set dell inventory box/date if it isn't already
		if ControlGetText( "HP ServiceCenter", "", "Edit14" ) = "" Then
			ControlCommand( "HP ServiceCenter", "", "Button35", "Check", "" )
			ControlSend( "HP ServiceCenter", "", "Edit14", "{F9}" )
			Sleep(250)
		EndIf
		
		; Set computername if the field is blank
		if ControlGetText( "HP ServiceCenter", "", "Edit5" ) Then ControlSetText( "HP ServiceCenter", "", "Edit5", $cname )
		
		; Set asset if the field is blank
		if ControlGetText( "HP ServiceCenter", "", "Edit17" ) Then ControlSetText( "HP ServiceCenter", "", "Edit17", $asset )
		
		; Record ID and clinical/non for safety
		if ControlGetText( "HP ServiceCenter", "", "Edit39" ) <> "" Then
			_ExcelWriteCell($objExcel, ControlGetText( "HP ServiceCenter", "", "Edit39" ), $count, 12 )
		EndIf
		
		if ControlCommand( "HP ServiceCenter", "", "Button76", "IsChecked", "" ) Then
			_ExcelWriteCell( $objExcel, "Yes", $count, 12 )
		Else
			_ExcelWriteCell( $objExcel, "No", $count, 12 )
		EndIf
		
		; Set clinical and shared if needed
		if $clinical Then
			ControlCommand( "HP ServiceCenter", "", "Button76", "Check", "" )
			ControlSetText( "HP ServiceCenter", "", "Edit36", "Yes" )
		; Set ID if there is one and it isn't clinical (basically training)
		ElseIf $id Then
			ControlSetText( "HP ServiceCenter", "", "Edit39", $id )
			ControlSetText( "HP ServiceCenter", "", "Edit36", "Yes" )
		; Common/not shared for everything else
		Else
			ControlCommand( "HP ServiceCenter", "", "Button76", "UnCheck", "" )
			ControlSetText( "HP ServiceCenter", "", "Edit36", "No" )
		EndIf
		
		; Set wireless if laptop (this should be the case 95% of the time)
		if ControlGetText( "HP ServiceCenter", "", "Edit15" ) = "Laptop" Then
			ControlCommand( "HP ServiceCenter", "", "Button82", "Check", "" )
		EndIf
		
		; Set location
		ControlFocus( "HP ServiceCenter", "", "Edit9" )
		ControlSetText( "HP ServiceCenter", "", "Edit9", $location )
		
		; Blank and auto-fill 'verified by'
		ControlSetText( "HP ServiceCenter", "", "Edit32", "" )
		Sleep(250)
		ControlFocus( "HP ServiceCenter", "", "Edit32" )
		ControlSend( "HP ServiceCenter", "", "Edit32", "{F9}" )
		Sleep(1000)
		
		; Save & close to go back to the search form
		ControlFocus( "HP ServiceCenter", "", "Edit32" )
		Send( "{F2}" )
		
		_ExcelBookSave( $objExcel )
		
	Next
	
	ProgressOff()
	_ExcelBookClose( $objExcel )
	
EndFunc

Func aSC_Retire()
	Dim $serial, $winTitle
	
	ProgressOn( "autoServiceCenter", "Retiring computers..." )
	
	For $line = 0 to _GUICtrlEdit_GetLineCount($edtSerial)
	
		$serial = _GUICtrlEdit_GetLine( $edtSerial, $line )

		ProgressSet( ($line/_GUICtrlEdit_GetLineCount($edtSerial))*100, "Current computer: " & $serial )
		
		if $serial = "" Then ExitLoop
	
		if NOT WinExists( "HP ServiceCenter - Search" ) Then WinWait( "HP ServiceCenter - Search" )
			
		WinActivate( "HP ServiceCenter - Search" )
		ControlSetText( "HP ServiceCenter - Search", "", "Edit1", $serial )
		Send( "{ENTER}" )

		$winTitle = "HP ServiceCenter - Configuration Item:  " & $serial & " - HP ServiceCenter"
		
		WinWaitActive( $winTitle )
		Sleep(2000)
		
		; location / contact info
		ControlSetText( $winTitle, "", "Edit4", "FIELD SERVICES" )
		ControlSetText( $winTitle, "", "Edit6", "" )
		ControlSetText( $winTitle, "", "Edit8", "" )
		ControlSetText( $winTitle, "", "Edit9", "" )
		ControlSetText( $winTitle, "", "Edit10", "PHK" )
		ControlSetText( $winTitle, "", "Edit12", "" )
		ControlSetText( $winTitle, "", "Edit13", "" )
		
		; retired status & verified by
		if ControlGetText( $winTitle, "", "Edit14" ) = "" Then
			ControlCommand( $winTitle, "", "Button35", "Check", "" )
			ControlSend( $winTitle, "", "Edit14", "{F9}" )
			Sleep(500)
		EndIf
		ControlSetText( $winTitle, "", "Edit34", "Retired" )
		ControlFocus( $winTitle, "", "Edit32" )
		ControlSetText( $winTitle, "", "Edit32", "" )
		Sleep(1000)
		ControlSend( $winTitle, "", "Edit32", "{F9}" )
		Sleep(2000)
		
		; save/close
		send( "{F2}" )
		
		Sleep(1000)
	
	Next

	ProgressOff()
	
EndFunc

Func aSC_Verify()
	Dim $serial, $location, $contact
	$serial = GUICtrlRead( $edtSerial )
	$location = GUICtrlRead( $edtLoc )
	$contact = GUICtrlRead( $edtContact )
	
	if NOT WinExists( "HP ServiceCenter - Configuration Item" ) Then
		WinActivate( "HP ServiceCenter - Search" )
		ControlSetText( "HP ServiceCenter - Search", "", "Edit1", $serial )
		Send( "{ENTER}" )
	Else
		$serial = ControlGetText( "HP ServiceCenter", "", "Edit3" )
	EndIf
	
	WinActivate( "HP ServiceCenter" )
	
	WinWaitActive( "HP ServiceCenter - Configuration", "", 10 )
	
	Sleep(500)
	if $contact <> ("Contact" AND "") Then
		ControlSetText( "HP ServiceCenter", "", "Edit4", $contact )
		ControlClick( "HP ServiceCenter", "", "Button28" )
		Sleep(1500)
	EndIf
	
	; Dell inventory
	ControlCommand( "HP ServiceCenter", "", "Button35", "Check", "" )
	ControlSend( "HP ServiceCenter", "", "Edit14", "{F9}" )
	Sleep(1000)
	
	; Verified by current user
	ControlSetText( "HP ServiceCenter", "", "Edit32", "" )
	ControlSend( "HP ServiceCenter", "", "Edit35", "{F9}" )
	Sleep(1000)
	
	; Set shared if clinical box is checked, otherwise no
	if ControlCommand( "HP ServiceCenter", "", "Button76", "IsChecked", "" ) Then
		ControlSetText( "HP ServiceCenter", "", "Edit36", "Yes" )
	Else
		ControlSetText( "HP ServiceCenter", "", "Edit36", "No" )
	EndIf
	
	; Set wireless if laptop (this should be the case 95% of the time)
	if ControlGetText( "HP ServiceCenter", "", "Edit15" ) = "Laptop" Then
		ControlCommand( "HP ServiceCenter", "", "Button82", "Check", "" )
	EndIf
	
	; location
	if $location <> ("Location" AND "") Then ControlSetText( "HP ServiceCenter", "", "Edit9", $location )
	
	TrayTip( "autoServiceCenter", "Configuration item " & $serial & " verified.", 2 )
	
EndFunc
